package org.com.mars.pojo.system;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.com.fw.consts.FwConst;
import org.com.fw.util.AppUtil;
import org.com.mars.dao.system.SysRoleAuthorityDao;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用户表
 * 
 * @author BIG
 *
 */
public class SysUser implements UserDetails {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * ID
	 */
	private String userId;

	/**
	 * 登陆用户名
	 */
	private String userAccount;

	/**
	 * 用户姓名
	 */
	private String userName;

	/**
	 * 用户密码
	 */
	private String userPassword;

	/**
	 * 用户描述
	 */
	private String userDesc;

	/**
	 * 是否被禁用（0：禁用，1：正常）
	 */
	private Boolean enabled;
	
	/**
	 * 是否是超级管理用户（0：普通，1：超级用户）
	 */
	private Boolean isSuper;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getUserDesc() {
		return userDesc;
	}

	public void setUserDesc(String userDesc) {
		this.userDesc = userDesc;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
	

	public Boolean getIsSuper() {
		return isSuper;
	}

	public void setIsSuper(Boolean isSuper) {
		this.isSuper = isSuper;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		SysRoleAuthorityDao sysRoleAuthorityDao = (SysRoleAuthorityDao)AppUtil.getBean(SysRoleAuthorityDao.class);
		Collection<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		List<String> authoritiesByUserId = sysRoleAuthorityDao.getAuthoritiesByUserId(userId);
		if (authoritiesByUserId != null) {
			for (String authority : authoritiesByUserId) {
				grantedAuthorities.add(new SimpleGrantedAuthority(authority));
			}
		}
		if (isSuper) {
			grantedAuthorities.add(FwConst.ROLE_GRANT_SUPER);
		}
		return grantedAuthorities;
	}

	@Override
	public String getPassword() {
		return userPassword;
	}

	@Override
	public String getUsername() {
		return userAccount;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SysUser other = (SysUser) obj;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}
	
}
